import { createRouter, createWebHistory } from "vue-router"

//制定路由规则

const routes = [
    {
        path: "/",
        name: "main",
        component: () => import("@/views/Main.vue"), // 动态导入的方式  按需加载
        redirect: "/home",
        children: [
            {
                path: "/home",
                name: "home",
                component: () => import("@/views/Home.vue"),
            },
            // 系统管理
            {
                path: "/system",
                name: "system",
                component: () => import("@/views/system/System.vue"),
                children: [
                    {
                        path: "user",
                        name: "user",
                        component: () => import("@/views/system/Users.vue"),
                    },
                    {
                        path: "role",
                        name: "role",
                        component: () => import("@/views/system/Roles.vue"),
                    }
                ]
            },
            // 内容管理
            {
                path: "/content",
                name: "content",
                component: () => import("@/views/content/Content.vue"),
                children: [
                    {
                        path: "article",
                        name: "article",
                        component: () => import("@/views/content/Articles.vue"),
                    },
                    {
                        path: "tag",
                        name: "tag",
                        component: () => import("@/views/content/Tags.vue"),
                    },
                    {
                        path: "comment",
                        name: "comment",
                        component: () => import("@/views/content/Comments.vue"),
                    }
                ]
            },
            // 工具管理
            {
                path: "/tool",
                name: "tool",
                component: () => import("@/views/tools/Tools.vue"),
                children: [
                    {
                        path: "center",
                        name: "center",
                        component: () => import("@/views/tools/Center.vue"),
                    },
                    {
                        path: "category",
                        name: "category",
                        component: () => import("@/views/tools/Categories.vue"),
                    }
                ]
            },
            // 业务运营
            {
                path: "/business",
                name: "business",
                component: () => import("@/views/business/Business.vue"),
                children: [
                    {
                        path: "project",
                        name: "project",
                        component: () => import("@/views/business/Projects.vue"),
                    },
                    {
                        path: "category",
                        name: "projectcat",
                        component: () => import("@/views/business/ProjectCats.vue"),
                    },
                    {
                        path: "lead",
                        name: "lead",
                        component: () => import("@/views/business/Leads.vue"),
                    }
                ]
            },
            // 日志管理
            {
                path: "/log",
                name: "log",
                component: () => import("@/views/log/Log.vue"),
                children: [
                    {
                        path: "syslog",
                        name: "syslog",
                        component: () => import("@/views/log/SysLog.vue"),
                    },
                    {
                        path: "oplog",
                        name: "oplog",
                        component: () => import("@/views/log/OpLog.vue"),
                    }
                ]
            }
        ]
    },
    {
        path: "/login",
        name: "login",
        component: () => import("@/views/UsernameLogin.vue"),
    },



]

//创建路由对象

const router = createRouter({
    history: createWebHistory(),
    routes
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
    // 获取token
    const token = localStorage.getItem('token')
    
    // 如果目标路由不是登录页且没有token，则重定向到登录页
    if (to.path !== '/login' && !token) {
        next('/login')
    } else {
        next()
    }
})

//导出路由对象

export default router